<template>
  <el-dialog width="1200px" :title="$t('m.inventory.operation')+inventoryTitle" :visible.sync="dialogVisible" :before-close="close" :close-on-click-modal="false">
    <el-form ref="form" label-width="120px">
      <!-- tool start -->
      <el-row>
        <template v-if="stocktype===3">
          <el-col :span="12">
            <el-form-item :label="$t('m.initInventory.stocktakecode')">
              <el-input maxlength="20" show-word-limit v-model="getInventoryDetail().initialstocktakecode" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('m.inventory.time')">
              <el-date-picker
                v-model="collisionDate"
                type="datetimerange"
                disabled
                value-format="yyyy-MM-dd HH:mm:ss"
                start-placeholder=""
                end-placeholder="">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </template>
        <template v-else>
          <el-col :span="24">
            <el-form-item :label="$t('m.initInventory.stocktakecode')">
              <el-input maxlength="20" show-word-limit v-model="getInventoryDetail().initialstocktakecode" disabled></el-input>
            </el-form-item>
          </el-col>

        </template>
      </el-row>
      <template v-if="[1,4].includes(stocktype)">
        <inventorySkuDetail @callback="$emit('callback');$emit('close')"/>
      </template>
      <template v-if="[2,3,5].includes(stocktype)">
        <inventoryLocationDetail @callback="$emit('callback');$emit('close')"/>
      </template>

      <!-- tool end -->
      <el-row>
        <el-col :span="24">
          <el-button type="primary" @click="setComplete">{{$t('m.quality.setting')}}</el-button>
          <el-button @click="$emit('close')">{{$t('m.close')}}</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import common from '@/utils/common'
export default {
  name:'handle-inventory-popup',
  props:['dialogVisible','detail'],
  data(){
    return {
      inventoryTitle:'', // title
      collisionDate:[], // 动碰时间段
      stocktype:'' // 盘点类型
    }
  },
  inject:{
    getStockOptions:{
      default:()=>[]
    },
    getInventoryDetail:{
      default:()=>{}
    }
  },
  mounted(){
    // 记录当前盘点类型
    this.stocktype = this.getInventoryDetail().stocktype
    // 根据盘点模式，显示对应的title
    let [stocktypeOptions] = this.getStockOptions()
    this.inventoryTitle = stocktypeOptions.find(item=> item.value === this.stocktype).label
    // 写入动碰时间段
    if(this.stocktype===3)
      this.collisionDate = [common.timestampToTime(this.getInventoryDetail().touchstartdate),common.timestampToTime(this.getInventoryDetail().touchenddate)]

  },
  components:{
    inventorySkuDetail:()=>import('@/components/initialInventory/sku/inventory-sku-detail'),
    inventoryLocationDetail:()=>import('@/components/initialInventory/location/inventory-location-detail')
  },
  methods:{
    close(){
      this.$emit('close')
    },
    // 设置完成btn -- 强制完成该盘点单
    setComplete(){
      // 存在未录入的checkqty，给予用户提示
      this.$confirm(`${this.$t('m.inventory.less')}${this.stocktype===1?'sku':this.$t('m.page.location')}，${this.$t('m.inventory.confirm')}`, this.$t('m.noticemessage'), {
        confirmButtonText:this.$t('m.confirm'),
        cancelButtonText:this.$t('m.cancel'),
        type:'warning'
      }).then(() => {
        this.submitData()
      }).catch(() => {
        this.callbackMessage(this.$t('m.inventory.cancel'),'info')
      })
    },
    submitData(){
      let options = {
        method:'get',
        url:`/initialstocktake/mandatoryfinish`,
        params:{
          initialstocktakeid:this.getInventoryDetail().initialstocktakeid
        }
      }
      this.$axios(options).then(({ message }) => {
        this.callbackMessage(message,'success',2000,()=>{
          this.$emit('callback')
          this.$emit('close')
        })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.el-date-editor{width: 100%;}
</style>